<?php

class kaixin001 {
	public  $useragent="Nokia"; //定义要模拟的浏览器名称
	public  $delaytime=3;//定义浏览间隔时间，以模仿人的浏览速度

	private $ch;	//CURL对象句柄
	private $cookie;	//保存Cookie的临时文件
	private $type="";	//操作标记
	private $page=array("listpage"=>"","parkpage"=>"","fishpage"=>"");//保存引用页地址
	private $data;	//临时数据保存地址

	private function gobyget($destpage,$refferpage)
	{
		//$destpage 目标页 $refferpage 引用页,
	sleep($this->delaytime);
	$ch = curl_init($this->ch);
    curl_setopt($ch, CURLOPT_URL, $destpage);
    curl_setopt($ch, CURLOPT_REFERER, $refferpage);
    curl_setopt($ch, CURLOPT_HTTPGET, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
    $data=curl_exec($ch);
    curl_close($ch);
    unset($ch);
    return $data;

	}

	private function gettips($data)
	{
	//得到执行后的结果信息
	preg_match_all( "/\<div class=\"tips\"\>(.*?)\<\/div\>/s",$data, $lists );
	return $lists[1][0];
	}
	private function gettips1($data)
	{
	//得到执行后的结果信息
	preg_match_all( "/\<div class=\"tips1\"\>(.*?)\<\/div\>/s",$data, $lists );
	return $lists[1][0];
	}
    function kaixin001() {

    }

    public function login($a,$b)
    {
    	//开始登录
    $d = tempnam('../tmp/', 'cookie.txt');  //创建随机临时文件保存cookie.
	$this->cookie=$d;

	//初始化Cookie
    $ch = curl_init("http://wap.kaixin001.com/");
    curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent);
    curl_exec($ch);
    curl_close($ch);
    unset($ch);
	//echo "<b>初始化登录.</b><br />";
	//ob_flush();
	//flush();

	$this->ch=$ch;
    $curlpost="email=".urlencode($a)."&password=".urlencode($b)."&from=&login=+%E7%99%BB+%E5%BD%95+";
    $ch = curl_init($this->ch);
    curl_setopt($ch, CURLOPT_URL, "http://wap.kaixin001.com/home/");
    curl_setopt($ch, CURLOPT_REFERER, "http://wap.kaixin001.com/");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlpost);
   	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, $this->useragent);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR,  $this->cookie);
    $data=curl_exec($ch);
    curl_close($ch);

    //echo "<b>开始登录.</b><br />";
	// ob_flush();
	// flush();
   //判断并显示是否登陆成功

	$ret=$this->gettips($data);
	if (strlen($ret)!=0)
	{
		echo $ret;
		unlink($d);

		exit;
	}

	//得到组件页面
	preg_match_all( "/\<a href=\"\/app\/mylist.php\?verify=(.*?)\" class=\"tdno\"\>/s",$data, $lists );
	$listpage="http://wap.kaixin001.com/app/mylist.php?verify=".$lists[1][0];
	$data=$this->gobyget($listpage,"http://wap.kaixin001.com/home/");
	//echo "<b>登录完毕.</b><br />";
	//ob_flush();
	//flush();

	//得到停车页面

    preg_match_all( "/parking\/index.php\?verify=(.*?)\"\>/s",$data, $lists );
	$parkpage="http://wap.kaixin001.com/parking/index.php?verify=".$lists[1][0];
	$this->page['parkpage']=$parkpage;
	$this->page['listpage']=$listpage;

    }
    private function park()
    {
	    $this->data=$this->gobyget($this->page['parkpage'],$this->page['listpage']);
	    $this->type="park";
		//echo "<b>开始获得数据.</b><br /><hr />";
		//ob_flush();
		//flush();
    }

	public function park_parkall()
	{
	//得到全部停车  注意判断是否可以全部停车
	$this->park();

	preg_match_all( "/park.php\?verify=(.*?)&amp;parkall=1/s",$this->data, $lists );

	if (count($lists[1])==0)
	{
		return ("距上次自动停车时间较短，请稍后再来停车。");

	}
	else
	{
		$parkall="http://wap.kaixin001.com/parking/park.php?verify=".$lists[1][0]."&parkall=1";


		$data=$this->gobyget($parkall,$this->page['parkpage']);

		return $this->gettips1($data).".";
	}

	}

	public function park_postall()
	{
		//得到可贴条的车，判断是否有可以贴条的车
	$this->park();

 	preg_match_all( "/post.php\?parkid=(.*?)\" title=\"\"/s",$this->data, $lists );

	if  (count($lists[1])==0)
	{
		return "没有可以贴条的车。";
	}
	else
	{
		foreach($lists[1] as $list)
		{

		$postpark="http://wap.kaixin001.com/parking/post.php?parkid=".$list;
		$postpark=str_replace("&amp;","&",$postpark);

		$data=$this->gobyget($postpark,$this->page['parkpage']);
		return $this->gettips1($data).".<br/>";

		}
	}
	}
	public function park_getmycar()
	{

		$this->park();
		preg_match_all("/\<\/div\>\<div class=\"list\"\>1(.*?)\<\/div\>\<div class=\"list\"\>\<font color=\"#669900\"\>我的车位/s",$this->data, $lists );
	 	$ret=preg_replace("/\<a href(.*?)\<\/a>/s"," ",$lists[1][0]);
	 	$ret=str_replace("<br/> <br/>","<br/>",$ret);
		return $ret;
	}

	public function park_getmypark()
	{

		$this->park();

		preg_match_all("/我的车位\<\/font\>\<\/div\>\<div class=\"list\"\>(.*?)\<\/div\>/s",$this->data, $lists );
	 	$ret=preg_replace("/\<img(.*?)贴条\"\/\>\<\/a\>\<br\/\>/s","",$lists[1][0]);

	 	$ret=preg_replace("/\<a href(.*?)\"\>/s"," ",$ret);
	 	$ret=str_replace("</a>","",$ret);
		return $ret;
	}

	public function park_getmycash()
	{

		$this->park();

		preg_match_all("/我的现金：(.*?)\<br\/\>/s",$this->data, $lists );

		return $lists[1][0];
	}


	public function logoff()
	{
		unset($this->ch);
		//echo "<br /><br /><b>开始清除服务器临时Cookie</b><hr />";
		unlink($this->cookie);//删除临时文件.
		//echo "<br /><b>清除服务器端Cookie完毕</b><hr />";
	}
}
?>